package com.maomao.maomaochong_i.fenLeiPackage;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.maomao.maomaochong_i.R;
import com.maomao.maomaochong_i.adapterPackage.MaoListAdapter;
import com.maomao.maomaochong_i.dataBean.AllItem;
import com.maomao.maomaochong_i.fenLeiPackage.Factory.FenLeiFactory;
import com.maomao.maomaochong_i.global.Show;
import com.maomao.maomaochong_i.quanxing.Adpater.BrandNewAdapter;
import com.maomao.maomaochong_i.quanxing.ShanPingShuJu;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by maomao on 2017/5/17.
 */

public class FenLeiPage {
    private Context context;
    private FenLeiPageNewTextView fenLeiPageNewTextView;
    private ListView listView;
    private FrameLayout fLayout;
    private FragmentManager fManager;
    private FragmentTransaction ft;
    private FenLeiPageGridFragment gFragment1, gFragment2, gFragment3, gFragment4, gFragment5,
            gFragment6, gFragment7, gFragment8, gFragment9, gFragment10, gFragment11,
            gFragment12, gFragment13;
    private ArrayList<AllItem> gridData1, gridData2, gridData3, gridData4, gridData5,
            gridData6, gridData7, gridData8, gridData9, gridData10, gridData11,
            gridData12, gridData13;
    private MaoListAdapter maoListAdapter;


    private String[] listName = {"全部商品", "趣品", "IPHONE7PLUS", "IPHONE7", "IPHONE6SPLUS",
            "IPHONE6S", "IPHONE6SE", "IPHONE6", "IPHONE5S", "IPHONE5C",  "笔记本电脑", "平板电脑",
            "配件"};//标题


    //默认构造方法
    public FenLeiPage() {

    }

    public View useFenLei(Context context, LayoutInflater inflater, ViewGroup container,
                          FragmentManager fManager) {
        this.context = context;
        this.fManager = fManager;

        //获得 XML布局
        View fenLeiView = inflater.inflate(R.layout.activity_fenlei, container, false);

        //获得相应组件
        fenLeiPageNewTextView = (FenLeiPageNewTextView) fenLeiView.findViewById(R.id.newTextView);
        listView = (ListView) fenLeiView.findViewById(R.id.flListView);
        fLayout = (FrameLayout) fenLeiView.findViewById(R.id.frameView);

        //给组件设置事件
        fenLeiPageNewTextView.setOnClickListener(listener);
        maoListAdapter = new MaoListAdapter(listName, context);
        listView.setAdapter(maoListAdapter);
        listView.setOnItemClickListener(itemlistener);

        //模拟一次点击，既进去后选择第一项
        listView.performItemClick(listView.getAdapter().getView(0, null, null),
                0, listView.getItemIdAtPosition(0));

        return fenLeiView;
    }

    //隐藏所有的Fragment
    private void hideAllFragment(FragmentTransaction ft){
        if (gFragment1 != null) ft.hide(gFragment1);
        if (gFragment2 != null) ft.hide(gFragment2);
        if (gFragment3 != null) ft.hide(gFragment3);
        if (gFragment4 != null) ft.hide(gFragment4);
        if (gFragment5 != null) ft.hide(gFragment5);
        if (gFragment6 != null) ft.hide(gFragment6);
        if (gFragment7 != null) ft.hide(gFragment7);
        if (gFragment8 != null) ft.hide(gFragment8);
        if (gFragment9 != null) ft.hide(gFragment9);
        if (gFragment10 != null) ft.hide(gFragment10);
        if (gFragment11 != null) ft.hide(gFragment11);
        if (gFragment12 != null) ft.hide(gFragment12);
        if (gFragment13 != null) ft.hide(gFragment13);
    }

    //点击搜索框
    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(context,FenLeiPageSearch.class);
            context.startActivity(intent);
        }
    };

    //点击侧边栏 ListView
    private AdapterView.OnItemClickListener itemlistener = new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            maoListAdapter.setSeclection(position);
            maoListAdapter.notifyDataSetChanged();

            //实现点击 listView显示相应的 fragment
            ft = fManager.beginTransaction();
            hideAllFragment(ft);

            switch (position){
                case 0:
                    if(gFragment1 == null){
                        //导入GridView中的数据
                        gridData1 = new ArrayList<AllItem>();
                        List<ArrayList<ShanPingShuJu>> list = FenLeiFactory.getList();
                        for (int leixing = 0; leixing < list.size(); leixing++) {
                            for (int number = 0; number < list.get(0).size(); number++)
                            gridData1.add(
                                    new AllItem(
                                            list.get(leixing).get(number).getTu(),
                                            list.get(leixing).get(number).getBiao(),
                                            list.get(leixing).get(number).getBiao()
                                    ));

                        }
                        gFragment1 = new FenLeiPageGridFragment(fManager, gridData1, context);
                        ft.add(R.id.frameView, gFragment1);
                    } else {
                        ft.show(gFragment1);
                    }
                    break;

                case 1:
                    if(gFragment2 == null){
                        //导入GridView中的数据
                        gridData2 = new ArrayList<AllItem>();
                        List<ShanPingShuJu> list = FenLeiFactory.getList(Show.INTERESSTION);
                        for (int i = 0; i < list.size(); i++) {
                            gridData2.add(new AllItem(list.get(i).getTu(),
                                    list.get(i).getBiao(), list.get(i).getJiage()));
                        }
                        gFragment2 = new FenLeiPageGridFragment(fManager, gridData2, context);
                        ft.add(R.id.frameView, gFragment2);
                    } else {
                        ft.show(gFragment2);
                    }
                    break;

                case 2:
                    if(gFragment3 == null){
                        //导入GridView中的数据
                        gridData3 = new ArrayList<AllItem>();
                        add(gridData3,8);
                        gFragment3 = new FenLeiPageGridFragment(fManager, gridData3, context);
                        ft.add(R.id.frameView, gFragment3);
                    } else {
                        ft.show(gFragment3);
                    }
                    break;

                case 3:
                    if(gFragment4 == null){
                        //导入GridView中的数据
                        gridData4 = new ArrayList<AllItem>();
                        add(gridData4,7);
                        gFragment4 = new FenLeiPageGridFragment(fManager, gridData4, context);
                        ft.add(R.id.frameView, gFragment4);
                    } else {
                        ft.show(gFragment4);
                    }
                    break;

                case 4:
                    if(gFragment5 == null){
                        //导入GridView中的数据
                        gridData5 = new ArrayList<AllItem>();
                        add(gridData5,5);
                        gFragment5 = new FenLeiPageGridFragment(fManager, gridData5, context);
                        ft.add(R.id.frameView, gFragment5);
                    } else {
                        ft.show(gFragment5);
                    }
                    break;

                case 5:
                    if(gFragment6 == null){
                        //导入GridView中的数据
                        gridData6 = new ArrayList<AllItem>();
                        add(gridData6,4);
                        gFragment6 = new FenLeiPageGridFragment(fManager, gridData6, context);
                        ft.add(R.id.frameView, gFragment6);
                    } else {
                        ft.show(gFragment6);
                    }
                    break;

                case 6:
                    if(gFragment7 == null){
                        //导入GridView中的数据
                        gridData7 = new ArrayList<AllItem>();
                        add(gridData7,6);
                        gFragment7 = new FenLeiPageGridFragment(fManager, gridData7, context);
                        ft.add(R.id.frameView, gFragment7);
                    } else {
                        ft.show(gFragment7);
                    }
                    break;

                case 7:
                    if(gFragment8 == null){
                        //导入GridView中的数据
                        gridData8 = new ArrayList<AllItem>();
                        add(gridData8,2);
                        gFragment8 = new FenLeiPageGridFragment(fManager, gridData8, context);
                        ft.add(R.id.frameView, gFragment8);
                    } else {
                        ft.show(gFragment8);
                    }
                    break;

                case 8:
                    if(gFragment9 == null){
                        //导入GridView中的数据
                        gridData9 = new ArrayList<AllItem>();
                        add(gridData9,1);
                        gFragment9 = new FenLeiPageGridFragment(fManager, gridData9, context);
                        ft.add(R.id.frameView, gFragment9);
                    } else {
                        ft.show(gFragment9);
                    }
                    break;

                case 9:
                    if(gFragment10 == null){
                        //导入GridView中的数据
                        gridData10 = new ArrayList<AllItem>();
                        add(gridData10,0);
                        gFragment10 = new FenLeiPageGridFragment(fManager, gridData10, context);
                        ft.add(R.id.frameView, gFragment10);
                    } else {
                        ft.show(gFragment10);
                    }
                    break;

                case 10:
                    if(gFragment11 == null){
                        //导入GridView中的数据
                        gridData11 = new ArrayList<AllItem>();
//                        add(gridData3,1);
                        gFragment11 = new FenLeiPageGridFragment(fManager, gridData11, context);
                        ft.add(R.id.frameView, gFragment11);
                    } else {
                        ft.show(gFragment11);
                    }
                    break;

                case 11:
                    if(gFragment12 == null){
                        //导入GridView中的数据
                        gridData12 = new ArrayList<AllItem>();
//                        add(gridData3,0);
                        gFragment12 = new FenLeiPageGridFragment(fManager, gridData12, context);
                        ft.add(R.id.frameView, gFragment12);
                    } else {
                        ft.show(gFragment12);
                    }
                    break;

                case 12:
                    if(gFragment13 == null){
                        //导入GridView中的数据
                        gridData13 = new ArrayList<AllItem>();
                        List<ShanPingShuJu> list = FenLeiFactory.getList(Show.PHONEACCESSORIES);
                        assert list != null;
                        for (int i = 0; i < list.size(); i++) {
                            gridData13.add(new AllItem(list.get(i).getTu(),
                                    list.get(i).getBiao(), list.get(i).getJiage()));
                        }
                        gFragment13 = new FenLeiPageGridFragment(fManager, gridData13, context);
                        ft.add(R.id.frameView, gFragment13);
                    } else {
                        ft.show(gFragment13);
                    }
                    break;
            }
            ft.commit();
        }
        private void add(ArrayList<AllItem> allItems, int number){
            List<ShanPingShuJu> newPhoneList = FenLeiFactory.getList(Show.NEWPHONE);
            List<ShanPingShuJu> sendPhone = FenLeiFactory.getList(Show.SENCONEDPHOE);
            assert newPhoneList != null;
            allItems.add(new AllItem(
                    newPhoneList.get(number).getTu(),
                    newPhoneList.get(number).getBiao(),
                    newPhoneList.get(number).getJiage()
            ));

            assert sendPhone != null;
            allItems.add(new AllItem(
                    sendPhone.get(number).getTu(),
                    sendPhone.get(number).getBiao(),
                    sendPhone.get(number).getJiage()
            ));
        }
    };
}
